package com.fingard.dsp.bank.directbank.cdrcb01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class CDRCB01Balance extends CDRCB01Base implements IBalance {

	public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
		// 余额查询返回ATS的对象
		RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
		retAts.writeStart();// 写返回ATS的统一头部信息
		try {
			CDRCB01Header header = new CDRCB01Header();
			header.tranCode = "001002";// 查询余额

			// 逐个查询
			while (p_req7506.readRecord()) {
				RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
				// 发送给银行报文的XML对象
				XmlTextWriter xmlWriter = new XmlTextWriter();
				xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"" + getCharset() + "\"?>");
				xmlWriter.writeStartElement("BCDL");
				xmlWriter.writeElementString("ACCT_NO", p_req7506.readedDetail.actNum);
				xmlWriter.writeEndElement();
				xmlWriter.writeEndDocument();

				System.out.println("xmlWriter.toXmlString():" + xmlWriter.toXmlString());
				String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), header, "ALL");
				// 如果返回tmpStrRet[0]表示返回connMsg，connMsg 为空则表示响应正常，
				// String tmpStrRet[] = { "",
				// "00000000|success|20150803102106185#<?xml version=\"1.0\"
				// encoding=\"UTF-8\"
				// ?><BCDL><ACCT_NO>021200000120010006853</ACCT_NO><ACCT_NAME>成都市桂溪建筑工程公司</ACCT_NAME><BALANCE>2302056.5</BALANCE><BALANCE_AVL>2302056.5</BALANCE_AVL></BCDL>"
				// };

				if (tmpStrRet[0].length() == 0) {
					String[] strRet = splitRetToString(tmpStrRet[1]);// 把结果拆分成String数组
					if (strRet[0].equals("00000000")) {
						StringBuilder tmpSbMsg = new StringBuilder();
						XmlTextReader tmpBkReader = loadBankRespXmlReader(strRet[3], tmpSbMsg);
						if (tmpBkReader != null) {
							if (tmpBkReader.read("/BCDL")) {
								tmpRetDetail.respCode = "0";
								// 获取币种
								tmpRetDetail.actCur = p_req7506.readedDetail.actCur;
								tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleText("ACCT_NO");
								tmpRetDetail.currBal = tmpBkReader.readedXElement.getSingleText("BALANCE");
								tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleText("BALANCE_AVL");
								Double frozenBal = Double.valueOf(tmpRetDetail.currBal)
										- Double.valueOf(tmpRetDetail.availBal);
								tmpRetDetail.frozenBal = new java.text.DecimalFormat("0.00").format(frozenBal);
							} else { // 处理失败respCode返回为-1
								tmpRetDetail.respCode = "-1";
								tmpRetDetail.respInfo = "返回报文格式不正确";
							}
						} else {
							tmpRetDetail.respCode = "-1";
							tmpRetDetail.respInfo = tmpSbMsg.toString();
						}
					} else {
						tmpRetDetail.respCode = "-1";
						tmpRetDetail.respInfo = strRet[1];
					}
				} else {
					tmpRetDetail.respCode = "-1";
					tmpRetDetail.respInfo = tmpStrRet[1];
				}
				retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
			}
		} catch (Exception e) {
			retAts.writeRespCode("-1");
			retAts.writeRespInfo(e.getMessage());
			e.printStackTrace();
		}
		retAts.writeEnd();
		return retAts;
	}

	@Override
	public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
		return null;
	}

}
